package com.owbtgad.common.utils;


import android.content.Context;

import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;

import org.jetbrains.annotations.NotNull;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


/**
 * 描述：埋点信息
 */
public class InsertHelp {

    private static String screenWidth;//屏幕宽度
    private static String screenHeight;//屏幕高度
    private static String os = "android";//操作系统，例如 ios
    private static String sdk = "android";//	SDK类型，例如android
    private static String sdkVersion;//SDK版本
    private static String attributes;//事件的具体属性，自定义


    private static String deviceId;//设备ID
    private static String userId;//用户ID
    private static String appVersion;//应用的版本，1.1.3
    private static String osVersion;//操作系统版本，例如 8.0
    private static String mfrs;//设备制造商
    private static String model;//设备类型
    private static String project = "default";//这条数据所属项目名
    private static String appChannel;//华为、小米、appStore等
    private static String imei;//IMEI设备号
    private static String idfa;//IDFA设备号
    private static String mac;//MAC地址

    private static String longitude;//经度
    private static String latitude;//纬度
    private static String appCode;//
    private static String sAuthCodeBeanjson;
    private static String TAG = "InsertHelp";
    //*******************************************8
    private static String iP;

    public static void setAppCode(String appCode) {
        InsertHelp.appCode = appCode;
    }

    public static String getImei() {
        return imei == null ? "" : imei;
    }

    public static void setImei(String imei) {
        InsertHelp.imei = imei;
    }

    public static String getAuthCodeBeanjson() {
        return sAuthCodeBeanjson;
    }

    public static void setAuthCodeBeanjson(String authCodeBeanjson) {
        sAuthCodeBeanjson = authCodeBeanjson;
    }

    public static String getLongitude() {
        return longitude == null ? "" : longitude;
    }

    public static void setLongitude(String longitude) {
        InsertHelp.longitude = longitude;
    }

    public static String getLatitude() {
        return latitude == null ? "" : latitude;
    }

    public static void setLatitude(String latitude) {
        InsertHelp.latitude = latitude;
    }

    public static String getScreenWidth() {
        return screenWidth == null ? "" : screenWidth;
    }

    public static void setScreenWidth(String screenWidth) {
        InsertHelp.screenWidth = screenWidth;
    }

    public static String getScreenHeight() {
        return screenHeight == null ? "" : screenHeight;
    }

    public static void setScreenHeight(String screenHeight) {
        InsertHelp.screenHeight = screenHeight;
    }

    public static String getOs() {
        return os == null ? "" : os;
    }

    public static void setOs(String os) {
        InsertHelp.os = os;
    }

    public static String getSdkVersion() {
        return sdkVersion == null ? "" : sdkVersion;
    }

    public static void setSdkVersion(String sdkVersion) {
        InsertHelp.sdkVersion = sdkVersion;
    }

    public static String getAttributes() {
        return attributes == null ? "" : attributes;
    }

    public static void setAttributes(String attributes) {
        InsertHelp.attributes = attributes;
    }

    public static String getDeviceId() {
        return deviceId == null ? "" : deviceId;
    }

    public static void setDeviceId(String deviceId) {
        InsertHelp.deviceId = deviceId;
    }

    public static String getUserId() {
        return userId == null ? "" : userId;
    }

    public static void setUserId(String userId) {
        InsertHelp.userId = userId;
    }

    public static String getAppVersion() {
        return appVersion == null ? "" : appVersion;
    }

    public static void setAppVersion(String appVersion) {
        InsertHelp.appVersion = appVersion;
    }

    public static String getOsVersion() {
        return osVersion == null ? "" : osVersion;
    }

    public static void setOsVersion(String osVersion) {
        InsertHelp.osVersion = osVersion;
    }

    public static String getMfrs() {
        return mfrs == null ? "" : mfrs;
    }

    public static void setMfrs(String mfrs) {
        InsertHelp.mfrs = mfrs;
    }

    public static String getModel() {
        return model == null ? "" : model;
    }

    public static void setModel(String model) {
        InsertHelp.model = model;
    }

    public static String getProject() {
        return project == null ? "" : project;
    }

    public static void setProject(String project) {
        InsertHelp.project = project;
    }

    public static String getAppChannel() {
        return appChannel == null ? "1" : appChannel;
    }

    public static void setAppChannel(String appChannel) {
        InsertHelp.appChannel = appChannel;
    }

    public static String getIdfa() {
        return idfa == null ? "" : idfa;
    }

    public static void setIdfa(String idfa) {
        InsertHelp.idfa = idfa;
    }

    public static String getMac() {
        return mac == null ? "" : mac;
    }

    public static void setMac(String mac) {
        InsertHelp.mac = mac;
    }

    public static String getSdk() {
        return sdk == null ? "" : sdk;
    }

    public static String getiP() {
        return iP == null ? "" : iP;
    }

    public static void setiP(String iP) {
        InsertHelp.iP = iP;
    }

    /**
     * 获取公共请求头参数
     *
     * @param context
     * @return
     */
    public static String getHttpHeads(Context context) {
        return getHttpHeads(context,null);
    }
    public static String getHttpHeads(Context context,Map<String, Object> map) {
        Map<String, Object> headers = getHeadMap(context);
        if (map!= null){
            headers.putAll(map);
        }
        return new Gson().toJson(headers);
    }


    @NotNull
    public static Map<String, Object> getHeadMap(Context context) {
        Map<String, Object> headers = new HashMap<>();
        headers.put("reqId", AppCache.getHeadId());
        headers.put("appVersion", appVersion);
        headers.put("netType", SystemInfoUtils.getNetType(context));

        headers.put("osType", "android");
        headers.put("osVersion", osVersion);
        headers.put("deviceType", getModel());
        headers.put("deviceId", deviceId);
        headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
        headers.put("channel", getAppChannel());
        headers.put("channelCode", MMKV.defaultMMKV().getString("referrer", "2"));
        headers.put("appCode", appCode);
        headers.put("clientIp", getiP());
        headers.put("region", "MX");
        // headers.put("referrer",MMKV.defaultMMKV().getString("referrer","def"));
        //headers.put("appLanguage", "Chinese");
        //headers.put("appLanguage", "English");
        headers.put("appLanguage", "Mexican");
        headers.put("token", AppCache.getAccessToken());
        if (sAuthCodeBeanjson != null) {
            headers.put("authCode", sAuthCodeBeanjson);
        }
        if (!AppCache.getUserId().isEmpty()) {
            headers.put("userId", AppCache.getUserId());
        }

        headers.put("screen", screenWidth + "x" + screenHeight);
        try {
            headers.put("wifiName", URLEncoder.encode(SystemInfoUtils.getConnectWifiSsid(context), "Utf-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return headers;
    }


    /**
     * 埋点
     */
    public static void insert(Context context, Map<String, Object> map) {


    }
}
